package com.easou.ps.master.channel;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.easou.ps.common.servicehandle.GBKCompactProtocol;
import com.easou.ps.thrift.search.rpc.WorkerRPC;
import com.easou.ps.thrift.search.types.IndexSearchRequest;
import com.easou.ps.thrift.search.types.WorkerSearchRequest;
import com.easou.ps.thrift.search.types.WorkerSearchResponse;
import com.easou.ps.thrift.search.types.WorkerSearchResult;


public class Client {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		callService("刘德华");
	}

	private static void callService(String q){
		WorkerSearchRequest req = new WorkerSearchRequest();
		IndexSearchRequest ireq = new IndexSearchRequest();
		Map<String, String> map = new HashMap<String, String>();
//		map.put("file", file);
//		map.put("req_type", "reload");
		
		ireq.queryStr = q;
		map.put("p", "1");
		map.put("wver", "3");
//		map.put("clearCache", "1");
		req.extendFields = map;
		req.request = ireq;
		//req.setQ("周杰伦");
//		req.setClearCache(1);
//		req.setPageNum(1);
//		req.setPageSize(6);
//		req.setDd("t");
//		req.setDebug(false);

		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e1) {
			return;
		}
		String ip = addr.getHostAddress().toString();//获得本机IP
		
		HashMap<Integer, String> ipMap = new HashMap<Integer, String>();
		ipMap.put(1, "121.9.222.164");
		ipMap.put(2, "183.60.215.79");
		ipMap.put(3, "113.105.248.178");
		ipMap.put(4, "113.105.248.179");
		ipMap.put(5, "121.9.222.165");

		TSocket socket = new TSocket(ipMap.get(2), 10000);	

		TTransport readTrans = new TFramedTransport(socket, 3000000);
		TTransport writeTrans = new TFramedTransport(socket, 3000000);
		TProtocol readProt = new GBKCompactProtocol(readTrans);
		TProtocol writeProt = new GBKCompactProtocol(writeTrans);

		try {
			if(!writeProt.getTransport().isOpen()){
				writeProt.getTransport().open();
			}
			
			if(!readProt.getTransport().isOpen()){
				readProt.getTransport().open();
			}
			
			WorkerRPC.search_args argss = new WorkerRPC.search_args();
			argss.setReq(req);
			
			writeProt.writeMessageBegin(new org.apache.thrift.protocol.TMessage("search", org.apache.thrift.protocol.TMessageType.CALL, 0));
			argss.write(writeProt);
			writeProt.writeMessageEnd();
			writeProt.getTransport().flush();
			
			WorkerRPC.search_result result = new WorkerRPC.search_result();
			org.apache.thrift.protocol.TMessage msg = readProt.readMessageBegin();
    		if (msg.type == org.apache.thrift.protocol.TMessageType.EXCEPTION) {
    			org.apache.thrift.TApplicationException x = org.apache.thrift.TApplicationException.read(readProt);
    			readProt.readMessageEnd();
    			throw x;
    		}
    		
    		result.read(readProt);
    		readProt.readMessageEnd();
			WorkerSearchResponse response = result.success;
			if(response == null){
				System.out.println("response == null");
			}
			System.out.println(response);

	    	
    	} catch (Exception e) {
//			System.out.println(getName() + ":ConnectionGuard run() caught exception:" + e);
			e.printStackTrace();
    	} finally {
//	    	pool.invalidateConnection(this);
//    		close();
    		readTrans.close();
    		writeTrans.close();
    		socket.close();
    	}
	}
}
